# Created by IntelliJ IDEA.
# User: apitluga
# Date: Aug 29, 2007
# Time: 1:43:45 PM
# To change this template use File | Settings | File Templates.
namespace :devenv do
    require 'rubygems'
    
    desc "creates the development database in MySQL"
    task :create_databases => :environment do
        ActiveRecord::Base.configurations.each do |key, config|
            if !(key =~ /production/)
                db = config["database"]
                puts "creating database #{db}"

                @charset   = ENV['CHARSET']   || 'utf8'
                @collation = ENV['COLLATION'] || 'utf8_general_ci' 

                ActiveRecord::Base.establish_connection(config.merge({'database' => nil}))
                ActiveRecord::Base.connection.execute("CREATE DATABASE #{db}") 
                ActiveRecord::Base.establish_connection(config)
            end
        end
    end

    # not working yet... last worked on by Tony and Sammy... and ideas?
    task :install_gems do
      # defaults to --no-rdoc --no-ri, set docs=(anything) to build docs
        docs = (ENV['docs'].nil? ? '--no-rdoc --no-ri' : '')
        #grab the list of gems/version to check
        gems = YAML.load_file 'config/gems.yml'

        gems.each do |gem|
            gem_spec = YAML.load(`gem spec #{gem[:name]}`)
            puts gem_spec
        end
#          # load the gem spec
#          gem_loaded = false
#          gem_loaded = require_gem gem[:name], gem[:version] rescue nil
#
#          # if forced
#          # or there is no gem_spec
#          # or the spec version doesn't match the required version
#          # or require_gem returns false
#          #    (return false also happens if the gem has already been loaded)
#          if ! ENV['force'].nil? or
#             ! gem_spec or
#             (gem_spec.version.version != gem[:version] and ! gem_loaded)
#
#            ret = system "gem install #{gem[:name]} -v #{gem[:version]} -y #{docs}"
#            # something bad happened, pass on the message
#            p $? unless ret
#          else
#            puts "#{gem[:name]} #{gem[:version]} already installed"
#          end
#        end
    end
end